Repository navigation
Know the invariants and the negated condition after a loop without break - #552
CatarinaGamboa wants to merge 2 commits into
Conversation
Codex review, round 1
No false negative found. The branch compiled successfully. The other tested violation cases—including labelled control flow, |
Codex round 1: what was done
Pushed in 1099fa5 (also reuses |
Codex review, round 2No false negative found. The branch reported refinement errors for the reachable violations in ExitPaths.java, EdgeConditions.java, and TryCatchFix.java. These cover labelled jumps, nested switch breaks, exception paths, for updates, for-each, condition side effects, and nested loops. The valid try/catch case produced no error, confirming the reported fix without reproducing the old “Variable could not be found” diagnostic.
What was done
No false negative in two rounds; the Codex loop for this PR ends here. |
Clean-code reviewAudit of this PR's own new code, clean-code-principles.
|
800c30e to
3775ff5
Compare
1099fa5 to
0d6cdd4
Compare
3775ff5 to
cfc266a
Compare
0d6cdd4 to
2049b41
Compare
After a while or for loop that only exits at its head, through its condition (no break leaves it and its condition writes no variable), the instances after the loop now satisfy the inferred invariants and the negated condition, when the condition is built of comparisons of the terms a candidate may mention. A do-while exits after its body, where the invariants of the head need not hold, so it keeps only the declared refinements, as do loops with a break. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… known A catch block starts from any value a variable had in the try block, including values recorded inside a loop body, which may refer to instances of a check of the body that was dropped. With facts after the loop, those references were no longer hidden by an unknown alternative and the check failed with "Variable could not be found" (already the case on main for a refined variable). The variable is now unknown in the catch block when such a value is among them. Also reuse LoopInvariants.condition for the loop exit check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2049b41 to
233721d
Compare
Stacked on #551; review/merge after it.
Fixes #545
Part of the loop invariant inference plan (#542).
What and why
After a loop, the variables it changes were havocked again: only their declared refinements were known, so after
int i = 0; while (i < n) i++;nothing saidi >= n. Now, after a loop that can only exit at its head through its condition, the instances after the loop get the invariants inferred in #551 and the negated condition:exitsAtHeadThroughCondition: awhile,foror for-each loop (not ado-while), with nobreakwhose target is the loop (labelled or not), and a condition that writes no variable.InvariantCandidates.conditiontranslates the condition when it is built of&&,||,!and comparisons (<,<=,>,>=,==,!=) of the terms a candidate may mention (int literals, int locals and parameters,+,-,*); otherwise only the invariants are added.LoopInvariants.afterLoopadds them to the post-loop instances (after the existing havoc), like the head facts.Also (found by the Codex review):
TryCheckerstarts a catch block from any value a variable had in the try block, including values recorded in loop-body checks that name join instances of a dropped check. The post-loop instance used to betrue, which hid them; with facts after the loop they surfaced asVariable #i_11 could not be found(already the case on main for a refined variable). The variable is now unknown in the catch block when such a value is among them.Soundness argument
while/forwithoutbreak, the code after the loop is reached only from that point (areturn,throwor labelled jump to an outer statement does not reach it), and the condition writes nothing, so the state after the loop is the head state where the condition was false: the invariants and the negated condition describe it.do-whileexits after its body and condition, where the head invariants need not hold (do { i = i + 5; } while (i < 3);keepsi <= 3at the head but exits withi == 5), so it is excluded. A condition with a side effect (while (i++ < 10)) exits in a state after the head, so it is excluded too.Before / after
int i = 0; while (i < n) i++; atLeast(i, n);(@Refinement("_ <= i") int n): beforeRefinement Error(onlyi's declared refinement known), afterCorrect.for (; i < 10; i++) { if (next()) continue; } ten(i);(_ == 10): beforeRefinement Error, afterCorrect(invarianti <= 10and!(i < 10)).Tests
CorrectLoopExitFacts(the first two fail on Infer loop invariants for the int locals a loop changes (Houdini) #551 alone):i >= nafter counting up withwhile;i == 10after aforwhose body has a continue; a loop in a try with a catch (spuriousNot Found Errorwithout theTryCheckerchange).ErrorLoopExitFacts: no negated condition after a loop withbreak; none after ado-while(the example above); none after a condition that increments (while (i++ < 10)exits withi == 11).InvariantCandidatesTest.translatesConditionsOverTheSameTerms:!,||,&&,==,!=translated; conditions with calls or a literaltruenot.Full suite passes locally.
🤖 Generated with Claude Code